package yt.kpool.activity.setting;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import yt.kpool.MainActivity;
import yt.kpool.R;
import yt.kpool.activity.personal.FastLookActivity;
import yt.kpool.adapter.AccountListviewAdapter;
import yt.kpool.adapter.PersonSelectListviewAdapter;
import yt.kpool.base.BaseApplication;
import yt.kpool.bean.ChildAccountBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.SPUtil;
import yt.kpool.util.TitleControl;
import yt.kpool.util.ToastUtil;
import yt.kpool.view.dialog.ErrorDialog;
import yt.kpool.view.dialog.LoadingDialog;

/**
 * 账户列表页
 */
public class AccountListActivity extends AppCompatActivity {
    @BindView(R.id.listview)
    ListView listview;
    private LoadingDialog loadingDialog;
    public AccountListviewAdapter accountListviewAdapter;//挖矿账户列表
    private List<ChildAccountBean.DataBean> personData;

    private Intent mainIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_list);
        ButterKnife.bind(this);
        initTitle();
        initListview();
//        getData();
        mainIntent=getIntent();
        setResult(5,mainIntent);
    }

    private void getData() {
        if (NetWorkUtil.check(this)) {


            SPUtil spUtil = new SPUtil(BaseApplication.context, BaseApplication.FILENAME);

            String id = spUtil.getString(BaseApplication.ID, "0");
            if (!"0".equals(id)) {
                loadingDialog.show();
                NetHelperNew.GetChildAccount(id, new HttpUtils.HttpCallback() {
                    @Override
                    public void onSuccess(String data) {
                        loadingDialog.dismiss();
                        Log.i("-->", "子账户列表：" + data);
                        ChildAccountBean childAccountBean = MyGson.getInstance().fromJson(data, ChildAccountBean.class);
                        if (childAccountBean.getType() == 1) {


                            if (childAccountBean.getData().size()>0){
                                personData.clear();
                                personData.addAll(childAccountBean.getData());
                                accountListviewAdapter.notifyDataSetChanged();
                            }else {
                                ToastUtil.showToast(AccountListActivity.this,"没有数据");
                            }
                        }
                    }

                    @Override
                    public void onError(String msg) {
                        super.onError(msg);

                        loadingDialog.dismiss();
                        ToastUtil.showToast(getApplicationContext(), msg);
                    }
                });
            }


        }

    }

    private void initListview() {
        personData = new ArrayList<>();
        accountListviewAdapter = new AccountListviewAdapter(this, personData);
        listview.setAdapter(accountListviewAdapter);

        accountListviewAdapter.notifyDataSetChanged();
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ChildAccountBean.DataBean dataBean = personData.get(i);
                EditAccountActivity.start(AccountListActivity.this,dataBean.getChildAccountId()+"",dataBean.getChildAccountName(),dataBean.getChildAccountRemark());
            }
        });

    }

    private void initTitle() {
        TitleControl titleControl = new TitleControl(this);
        titleControl.setTitle("挖矿账户");
        titleControl.setRightImg(R.mipmap.jiahao, new TitleControl.OnClickRight() {
            @Override
            public void onRight() {

                startActivityForResult(new Intent(AccountListActivity.this, AddAcountActivity.class), 21);

            }
        });
        loadingDialog = new LoadingDialog(this, 0);
    }




    @Override
    protected void onResume() {
        super.onResume();
        personData.clear();
        accountListviewAdapter.notifyDataSetChanged();
        getData();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 21 && resultCode == 10) {
           // mainIntent.putExtra("add","1");
        }
    }
}
